/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bkwl_log.h"
#include "iot_import.h"
#include "iot_export.h"
#include "iot_export_mqtt.h"

typedef void (*bkwl_mqtt_recive_cb_t)(const char *topic, char *data_buf, uint32_t data_len);

extern int mqtt_client_example(const char *host_name, const char *username, const char *password, const char *topic);

extern int bkwl_mqtt_base_client_init(const char *host_name, const char *username,
									  const char *password, const char *client_id, const char *topic, bkwl_mqtt_recive_cb_t recive_cb);

extern int mqtt_cmd_msg_send(char *topic, char *msg);

void bkwl_mqtt_recive_handle(const char *topic, char *data_buf, uint32_t data_len)
{
	tprintf("bkwl_mqtt_recive_handle topic=%s ,data_len=%d\n%s\n", topic, data_len, data_buf);
}
int bkwl_mqtt_client_init(const char *host_name, const char *username,
						  const char *password, const char *topic)
{
	int rc = 0;
	// tprintf("bkwl_mqtt_client_init entry\n");
	tprintf("bkwl_mqtt_client_init host_name=%s\n", host_name);
	tprintf("bkwl_mqtt_client_init username=%s\n", username);
	tprintf("bkwl_mqtt_client_init password=%s\n", password);
	tprintf("bkwl_mqtt_client_init topic=%s\n", topic);

	char *client_id = "aaaa";

	tprintf("bkwl_mqtt_client_init 11111\n");
	bkwl_mqtt_base_client_init(host_name, username,
							   password, client_id, topic, bkwl_mqtt_recive_handle);

	tprintf("bkwl_mqtt_client_init 222222\n");

	// rc = mqtt_client_example(host_name, username,
	// 						 password, topic);
	bkwl_main->status.mqtt_connect = 1;
	// tprintf("bkwl_mqtt_client_init exit\n");
	return rc;
}

int bkwl_mqtt_cmd_msg_send(char *topic, char *msg)
{
	int rc = 0;

	// tprintf("bkwl_mqtt_cmd_msg_send entry\n");
	tprintf("\nmqtt send topic=%s\n", topic);
	tprintf("%s\n\n\n", msg);
	rc = mqtt_cmd_msg_send(topic, msg);
	// tprintf("bkwl_mqtt_cmd_msg_send exit\n");
	return rc;
}
